<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /**
 * @param {number[][]} mat
 * @return {number[][]}
 */
        var updateMatrix = function (mat) {
            let m = mat.length;
            let n = mat[0].length;
            let visited = [];
            let res = [];
            for (let i = 0; i < m; i++) {
                visited[i] = [];
                res[i] = [];
                for (let j = 0; j < n; j++) {
                    visited[i][j] = 0;
                    res[i][j] = 0;
                }
            }
            let arr = [];
            let dir = [[-1, 0], [0, 1], [1, 0], [0, -1]];
            // console.log(res);
            for (let i = 0; i < m; i++) {
                for (let j = 0; j < n; j++) {
                    if (mat[i][j] === 0) {
                        arr.push([i, j]);
                        visited[i][j] = 1;
                    }
                }
            }
            console.table('arr');
            console.table(arr);
            console.table('visited');
            console.table(visited);
            while (arr.length) {
                let [x, y] = arr.shift();
                console.error('x,y',x,y);
                for (let i = 0; i < dir.length; i++) {
                    let xToMove = x + dir[i][0];
                    let yToMove = y + dir[i][1];
                    if (xToMove >= 0 && xToMove <= m - 1 && y >= 0 && y <= n-1 && visited[xToMove][yToMove] === 0) {
                        console.log('xToMove yToMove', xToMove, yToMove)

                        res[xToMove][yToMove] = res[x][y] + 1;
                        arr.push([xToMove, yToMove]);
                        visited[xToMove][yToMove] = 1;
                        console.table(arr);

                    }
                }
            }
            return [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
        };
        updateMatrix([[0,0,0],[0,1,0],[0,0,0]]);
    </script>
</body>

</html>